import { _decorator, Component, Node, Graphics, tween, Color, math, Vec3 } from 'cc';
import { PoolManager } from './PoolManager';
const { ccclass, property } = _decorator;

@ccclass('CircleEffectWithGraphics')
export class CircleEffectWithGraphics extends Component {
    graphics: Graphics = null!; // 需要拖拽Graphics组件

    private radius: number = 10; // 初始半径
    private maxRadius: number = 200; // 最大半径
    private fadeDuration: number = 1; // 动画时长
    private opacity: number = 255; // 初始不透明度
    private width: number = 20;

    private _curRadius: number = 0;
    private _curOpacity: number = 255;

    init(parent: Node, pos: Vec3, radius: number, maxRadius: number, duration: number, width: number) {
        this.node.parent = parent;
        this.node.setWorldPosition(pos);
        // 确保图形组件存在
        if (!this.graphics) {
            this.graphics = this.addComponent(Graphics);
        }
        this.radius = radius;
        this.maxRadius = maxRadius;
        this.fadeDuration = duration;
        this.width = width;
        this._curRadius = this.radius;
        this._curOpacity = 255;
    }

    update(dt: number) {
        // 动态更新圆的半径和透明度
        this._curRadius += dt * (this.maxRadius - this.radius) / this.fadeDuration;
        this._curOpacity -= dt * this.opacity / this.fadeDuration;

        this.drawCircle(math.clamp(this._curRadius, this.radius, this.maxRadius));
        // 每一帧更新后，绘制一个更大的圆并改变透明度
        this.graphics.lineWidth = 5;  // 线条宽度
        this.graphics.strokeColor = new Color(255, 255, 255, math.clamp(this._curOpacity, 0, this.opacity)); // 白色，透明度逐渐变化

        if (this._curOpacity < 0) {
            PoolManager.Instance.put(this.node);
        }
    }

    private drawCircle(radius: number) {
        // 清除之前的图形
        this.graphics.clear();

        // 开始绘制圆形
        this.graphics.lineWidth = this.width;
        this.graphics.moveTo(radius, 0);
        this.graphics.circle(0, 0, radius);  // 圆心在(0, 0)，半径为radius
        this.graphics.stroke();  // 绘制圆形
    }
}


